from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QFont, QFontMetrics, QPainter,QPixmap
from PyQt5.QtWidgets import QApplication, QLabel, QWidget
from modules import *

class SongInfoCard(QWidget):
    """ 滚动字幕 """

    def __init__(self, songName, artist, parent=None):
        super().__init__(parent)
        self.songName = songName
        self.artist = artist
        # 实例化定时器
        self.timer = QTimer(self)
        # 设置刷新时间和移动距离
        self.timeStep = 20
        self.moveStep = 1
        self.songCurrentIndex = 0
        self.artistCurrentIndex = 0
        # 设置字符串溢出标志位
        self.isSongNameAllOut = False
        self.isArtistAllOut = False
        # 设置两段字符串之间留白的宽度
        self.spacing = 25
        # 初始化界面
        self.initWidget()

        CommonSignal.changeSongInfoSignal.connect(self.updateData)

    def initWidget(self):
        """ 初始化界面 """
        self.setFixedHeight(90)
        self.setAttribute(Qt.WA_StyledBackground)
        # 调整窗口宽度
        self.adjustWindowWidth()
        # 初始化定时器
        self.timer.setInterval(self.timeStep)
        self.timer.timeout.connect(self.updateIndex)
        # 只要有一个字符串宽度大于窗口宽度就开启滚动：
        # if self.isArtistTooLong or self.isSongNameTooLong:
        #     self.timer.start()

    def getTextWidth(self):
        """ 计算文本的总宽度 """
        songFontMetrics = QFontMetrics(QFont('Microsoft YaHei', 14, 400))
        self.songNameWidth = songFontMetrics.width(self.songName)
        artistFontMetrics = QFontMetrics(QFont('Microsoft YaHei', 12, 500))
        self.artistWidth = artistFontMetrics.width(self.artist)

    def adjustWindowWidth(self):
        """ 根据字符串长度调整窗口宽度 """
        self.getTextWidth()
        maxWidth = max(self.songNameWidth, self.artistWidth)
        # 判断是否有字符串宽度超过窗口的最大宽度
        self.isSongNameTooLong = self.songNameWidth > 250
        self.isArtistTooLong = self.artistWidth > 250

        # 设置窗口的宽度
        # self.setFixedWidth(min(maxWidth, 270))

    def updateIndex(self):
        """ 更新下标 """
        self.update()
        self.songCurrentIndex += 1
        self.artistCurrentIndex += 1
        # 设置下标重置条件
        resetSongIndexCond = self.songCurrentIndex * self.moveStep >= self.songNameWidth + self.spacing * self.isSongNameAllOut
        resetArtistIndexCond = self.artistCurrentIndex * self.moveStep >= self.artistWidth + self.spacing * self.isArtistAllOut
        # 只要条件满足就要重置下标并将字符串溢出置位，保证在字符串溢出后不会因为留出的空白而发生跳变
        if resetSongIndexCond:
            self.songCurrentIndex = 0
            self.isSongNameAllOut = True
        if resetArtistIndexCond:
            self.artistCurrentIndex = 0
            self.isArtistAllOut = True

    def paintEvent(self, e):
        """ 绘制文本 """
        # super().paintEvent(e)
        painter = QPainter(self)
        painter.setPen(Qt.white)
        # 绘制歌名
        painter.setFont(QFont('Microsoft YaHei', 14))
        if self.isSongNameTooLong:
            self.timer.start()
            # 实际上绘制了两段完整的字符串
            # 从负的横坐标开始绘制第一段字符串
            painter.drawText(self.spacing * self.isSongNameAllOut - self.moveStep *
                             self.songCurrentIndex, 36, self.songName)
            # 绘制第二段字符串
            painter.drawText(self.songNameWidth - self.moveStep * self.songCurrentIndex +
                             self.spacing * (1 + self.isSongNameAllOut), 36, self.songName)
        else:
            self.timer.stop()
            painter.drawText(0, 36, self.songName)

        # 绘制歌手名
        painter.setFont(QFont('Microsoft YaHei', 12, 500))
        if self.isArtistTooLong:
            painter.drawText(self.spacing * self.isArtistAllOut - self.moveStep *
                             self.artistCurrentIndex, 64, self.artist)
            painter.drawText(self.artistWidth - self.moveStep * self.artistCurrentIndex +
                             self.spacing * (1 + self.isArtistAllOut), 64, self.artist)
        else:
            painter.drawText(0, 64, self.artist)

    def updateData(self, songName, artist):
        self.songName = songName
        self.artist = artist
        self.adjustWindowWidth()
        self.update()
        # self.updateIndex()